using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using ValidationFramework.Reflection;
using ValidationFramework.Utilities;

namespace ValidationFramework.Configuration
{
	public class ConfigureRuleExpression : IConfigureRuleExpression
	{
		private TypePolicyRepository _typeCache;
		private MethodPolicyRepository _methodCache;

		internal ConfigureRuleExpression(TypePolicyRepository typeCache, MethodPolicyRepository methodCache)
		{
			_typeCache = typeCache;
			_methodCache = methodCache;
		}

		// --- Methods | IAddRuleExpression

		public void AddRuleTo<TTType>(Expression<Func<TTType, object>> property, Rule rule, RuleScope scope)
		{
			var TypeKey = new TypeKey(typeof(TTType).TypeHandle);
			var propertyKey = new PropertyKey(TypeKey, ExprUtil.GetMemberNameFromExpression(property));
			_typeCache.AddPropertyRule(propertyKey, rule, scope);
		}

		public void AddPropertuRule(PropertyKey key, Rule rule, RuleScope scope)
		{
			_typeCache.AddPropertyRule(key, rule, scope);
		}

		public void AddFieldRule(FieldKey key, Rule rule, RuleScope scope)
		{
			_typeCache.AddFieldRule(key, rule, scope);
		}

		public void AddParameterRule(ParameterKey key, Rule rule, RuleScope scope)
		{
			_methodCache.AddParameterRule(key, rule, scope);
		}
	}
}
